﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.FTPClient;
namespace WPFExcelReport
{
    struct vvPACK
    {
        public string PackNr;
        public SortedList<string, DataRow> WBBSTnr;
    }
    struct vvVKPOS
    {
        public DataRow dwVKPOS;
        public SortedList<string, vvPACK> listVVPACK;
    }
    class CADMAPINTEXReport : BaseReport
    {
        #region ----------------PROPERTIES----------------------------------
        byte[] b;
        string WBBAUFKRS;
        string WBBAUFLFD;
        string WBBAUFPOS;
        string WBBALIEFSCH;
        string VKAUFNum = "";
        string WBBTAX;
        string DocType;
        string CellName = "";
        string CellValue = "";
        string strCompany;
        string orderComments = "备注：";
        string strExcelFile;
        public string Customer = "";
        public string Address = "";
        string DateCurrent = "";
        public string PrintType = "JustExcel";
        public List<vvVKPOS> listVKPOS = new List<vvVKPOS>();
        public FTPClient ftpClient;
        #endregion
        public CADMAPINTEXReport()
            : base()
        {

        }
        public CADMAPINTEXReport(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            DocType = dicParamater["DocType"].ToString().Trim();
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
            DateCurrent = DateTime.Now.Year.ToString() + "年" + DateTime.Now.Month.ToString() + "月" + DateTime.Now.Day.ToString() + "日";
        }

        private int FillInEXCEL()
        {
            int result = 1;
            try
            {
                DataTable tbWBBST = dsSource.Tables["WBBST"];

                DataSet setTemp = new DataSet();
                int x = 0, startx = 10;
                int y = 2;
                int page = 0;
                int rowsperpage = 25;
                int pagestart = 10;
                x = startx;
                pagestart = (page) * rowsperpage + startx;
                /*
                 1.针对其中一张发货单
                 * 1.1建立一个结构，包括销售单行，品种，包装总数，总长度，总重量，长度单位
                 * 1.1.1包括包装列表，
                 * 1.1.2每个包装中包括布卷明细
                 * 1.1找到所有的销售单行
                 * 1.2遍历所有的销售单行
                 * 1.3找到对应的布卷，按照包装号排序
                 * 1.4每包装累加
                 2.列举所包含的销售单行，品种，总数量，包装数，每件数量
                 * 2.1查询布卷时，按照销售单，行，品种，箱号，布卷号排序
                 * 2.3遍历布卷，如果发现新的销售单行
                 3.列举每件内布卷的明显，每件总数
                 */

                VV06Header();
                int row = 10;
                string VKPMEEH = "";
                //int packrow = 1999;
                SortedList<string, vvVKPOS> ListVKPOS = new SortedList<string, vvVKPOS>();
                vvVKPOS vkpos = new vvVKPOS();
                vvPACK pack = new vvPACK();
                int packnr;
                string processvkpos = "";
                string processpack = "";
                string processstknr = "";
                string vkposnr = "", VKPAUFKRS, VKPAUFLFD, VKPAUFPOS;
                double STKLG = 0.0;
                double vkplg = 0;
                double vkpkg = 0;
                string STKNR;
                var queryvkp = from dwVKPOS in dsSource.Tables["VKPOS"].AsEnumerable()
                               orderby dwVKPOS["VKPAUFKRS"].ToString(), dwVKPOS["VKPAUFLFD"].ToString(), dwVKPOS["VKPAUFPOS"].ToString()
                               select dwVKPOS;
                #region ==============GetVKPOSLineTotal==========
                foreach (DataRow dwVKPOS in queryvkp)
                {
                    row++;
                    vkpos = new vvVKPOS();
                    vkpos.dwVKPOS = dwVKPOS;
                    vkpos.listVVPACK = new SortedList<string, vvPACK>();
                    VKPAUFKRS = dwVKPOS["VKPAUFKRS"].ToString();
                    VKPAUFLFD = dwVKPOS["VKPAUFLFD"].ToString();
                    VKPAUFPOS = dwVKPOS["VKPAUFPOS"].ToString();
                    vkposnr = VKPAUFKRS + VKPAUFLFD + VKPAUFPOS;

                    if (processvkpos.Contains(vkposnr + ";"))
                        continue;

                    processvkpos += vkposnr + ";";
                    ListVKPOS.Add(vkposnr, vkpos);
                    var query = from dwWBBST in tbWBBST.AsEnumerable()
                                where dwWBBST["WBBAUFKRS"].ToString() == VKPAUFKRS
                                & dwWBBST["WBBAUFLFD"].ToString() == VKPAUFLFD
                                & dwWBBST["WBBAUFPOS"].ToString() == VKPAUFPOS
                                orderby
                                dwWBBST["WBBURLIEFSCH"].ToString(),
                                dwWBBST["WBBBEM"].ToString().Substring(0, 3),
                                dwWBBST["WBBSTKNR"].ToString()
                                select dwWBBST;
                    foreach (DataRow dwWBBST in query)
                    {
                        STKNR = dwWBBST["WBBSTKNR"].ToString();
                        if (processstknr.Contains(STKNR + ";"))
                            continue;
                        processstknr += STKNR + ";";
                        packnr = 0;
                        try
                        {
                            packnr = System.Convert.ToInt32(Right("0000" + dwWBBST["WBBBEM"].ToString().Substring(0, 3), 4));
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(STKNR + "件号有误!");
                        }
                        if (packnr == 0)
                            packnr = 1;
                        string wv02packnr = dwWBBST["WBBPACKNR"].ToString();
                        if (System.Convert.ToInt32(wv02packnr) > 1)
                            packnr = System.Convert.ToInt32(wv02packnr);
                        if (!processpack.Contains(vkposnr + ";" + packnr + ";"))
                        {
                            pack = new vvPACK();
                            pack.PackNr = Right("0000" + packnr.ToString(), 4);
                            pack.WBBSTnr = new SortedList<string, DataRow>();
                            pack.WBBSTnr.Add(STKNR, dwWBBST);
                            vkpos.listVVPACK.Add(pack.PackNr, pack);
                            processpack += vkposnr + ";" + packnr + ";";
                        }
                        else
                        {
                            foreach (var vpack in vkpos.listVVPACK)
                            {
                                int packnrx = System.Convert.ToInt32(vpack.Key);
                                if (packnrx == packnr)
                                {
                                    ((vvPACK)vpack.Value).WBBSTnr.Add(STKNR, dwWBBST);
                                }
                            }
                        }
                    }
                }
                #endregion
                #region==============WritePosition==========
                row = 10;
                int rowx = 10;

                double ratio = 1 / 0.9144;
                int vkpbox = 0;
                int vkprolls = 0;
                int boxrolls = 0;
                int ttlbox = 0;
                int ttlrolls = 0;
                foreach (var vkpostmp in ListVKPOS)
                {

                    ratio = 1 / 0.9144;
                    double dellength = 0;
                    double delweight = 0.0;
                    vkpos = (vvVKPOS)vkpostmp.Value;
                    if (vkpos.listVVPACK.Count == 0)
                        continue;
                    row++;
                    rowx = row;
                    DataRow dwvkp = vkpos.dwVKPOS;
                    string KRS = dwvkp["VKPAUFKRS"].ToString();
                    string LFD = System.Convert.ToInt64(dwvkp["VKPAUFLFD"].ToString()).ToString();
                    string POS = System.Convert.ToInt64(dwvkp["VKPAUFPOS"].ToString()).ToString();
                    VKPMEEH = dwvkp["VKPMEEH"].ToString();
                    #region=====Delivery note========
                    exAgent.SetCellValue(rowx + 1, 2, KRS + "-" + LFD + "-" + POS);
                    exAgent.SetCellValue(rowx + 1, 7, dwvkp["VKPART"].ToString());
                    exAgent.SetCellValue(rowx + 1, 10, dwvkp["VKPFARBE"].ToString());
                    exAgent.SetCellValue(rowx + 1 + 1, 2, "贵司品种/色号:" + dwvkp["VKPKDARTNR"].ToString() + "/" + dwvkp["VKPKDFARBNR"].ToString());
                    #endregion

                    if (VKPMEEH.Contains("M"))
                        ratio = 1;
                    row++;
                    vkprolls = 0;
                    vkpbox = 0;
                    row++;
                    foreach (var packtmp in vkpos.listVVPACK)
                    {
                        vkpbox++;
                        boxrolls = 0;
                        double packleng = 0.0;
                        double packweight = 0.0;
                        double stkweight = 0.0;

                        string pclocation = "";

                        foreach (var pcs in ((vvPACK)packtmp.Value).WBBSTnr)
                        {
                            boxrolls++;
                            DataRow dwPc = (DataRow)pcs.Value;
                            pclocation = dwPc["WBBLO"].ToString() + "-" + dwPc["WBBLB"].ToString() + "-" + dwPc["WBBLP"].ToString();
                            STKLG = System.Math.Round(ratio * 0.001 * System.Convert.ToDouble(dwPc["WBBSSTKLG"].ToString()), 0);
                            stkweight = System.Math.Round(0.001 * System.Convert.ToDouble(dwPc["WBBIGEW"].ToString()), 1);
                            packleng += STKLG;
                            packweight += stkweight;
                            vkplg += STKLG;
                            vkpkg += stkweight;
                            row++;
                            exAgent.SetCellValue(row, 13, dwPc["WBBSTKNR"].ToString());
                            exAgent.SetCellValue(row, 18, "'" + STKLG);
                            exAgent.SetCellValue(row, 23, "'" + stkweight);
                            exAgent.HideRows(row, 1, row, 1);
                        }
                        vkprolls += boxrolls;
                        dellength += packleng;
                        delweight += packweight;
                        row++;
                        exAgent.SetCellValue(row, 5, "库位:" + pclocation);
                        exAgent.SetCellValue(row, 10, "件号:");
                        exAgent.SetCellValue(row, 13, "'" + System.Convert.ToInt32(packtmp.Key) + "#:" + boxrolls + "卷");
                        exAgent.SetCellValue(row, 18, "'" + packleng);
                        exAgent.SetCellValue(row, 23, "'" + packweight);
                        exAgent.HideRows(row, 1, row, 1);
                    }
                    ttlrolls += vkprolls;
                    ttlbox += vkpbox;
                    exAgent.SetCellValue(rowx + 1, 13, "'" + vkpbox + "件");
                    exAgent.SetCellValue(rowx + 1, 16, "'" + vkprolls + "匹");
                    exAgent.SetCellValue(rowx + 1, 18, "'" + dellength);
                    exAgent.SetCellValue(rowx + 1, 23, "'" + delweight);
                    exAgent.ShowRows(rowx + 1, 1, rowx + 1, 1);
                }
                #endregion
                #region===============Footer================
                exAgent.SetCellValue(row + 2, 8, "汇总:");
                exAgent.SetCellValue(row + 2, 12, "'" + ttlbox + "件");
                exAgent.SetCellValue(row + 2, 15, "'" + ttlrolls + "匹");
                exAgent.SetCellValue(row + 2, 18, "'" + vkplg + VKPMEEH.Trim());
                exAgent.SetCellValue(row + 2, 23, "'" + vkpkg + "kg");
                exAgent.ShowRows(row + 2, 1, row + 2, 1);
                string footer = "制单：                      审单:	                        发货:                      核对:	                     司机:                   收货:                     ";
                exAgent.SetCellValue(row + 4, 2, footer);
                exAgent.ShowRows(row + 4, 1, row + 4, 1);
                result = row + 10;
                #endregion
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return result;
        }
        private void VV06Header()
        {
            try
            {
                #region=========Variable==============================
                int row = 7;
                //int packrow = 1999;
                DataTable tbVVLIEF = dsSource.Tables["VVLIEF"];
                DataTable tbKUNDADR = dsSource.Tables["KUNDADR"];
                DataTable tbKDSTAMM = dsSource.Tables["KDSTAMM"];
                DataTable tbVKKOPF = dsSource.Tables["VKKOPF"];
                DataTable tbVKPOS = dsSource.Tables["VKPOS"];
                DataTable tbKDANSP = dsSource.Tables["KDANSP"];
                DataRow dwVVLIEF = tbVVLIEF.Rows[0];
                #endregion

                #region=========收件人地址==============================
                string receiver = "";
                string KUNDNR = dwVVLIEF["VVKUNDNRE"].ToString();
                row = 7;
                receiver += "客户编号:" + System.Convert.ToInt64(KUNDNR).ToString().Trim();
                //exAgent.SetCellValue(row, 2, "客户编号:" + System.Convert.ToInt64(KUNDNR).ToString().Trim());
                /*var query = from dwKDSTAMM in tbKDSTAMM.AsEnumerable()
                            where dwKDSTAMM["KDKUNDNR"].ToString() == KUNDNR
                            select dwKDSTAMM;
                foreach (DataRow dwKDSTAMM in query)
                {
                    row = 7;
                    string KDNAME1 = GetGB2312Value(dwKDSTAMM["KDNAME1"].ToString().Trim());
                    exAgent.SetCellValue(row, 7, KDNAME1);
                }*/
                string phoneR = ""; 
                var query = from dwKUNDDADR in tbKUNDADR.AsEnumerable()
                        where dwKUNDDADR["KDAKUNDNR"].ToString() == KUNDNR
                        & dwKUNDDADR["KDAADRSL"].ToString() == dwVVLIEF["VVADRSLL"].ToString()
                        select dwKUNDDADR;
               
                foreach (DataRow dwKUNDDADR in query)
                {                  
                    phoneR = "";
                    receiver+="    公司名:"+GetGB2312Value(dwKUNDDADR["KDANAME1"].ToString().Trim())+Environment.NewLine; 
                    string KDASTRASSE1 = GetGB2312Value(dwKUNDDADR["KDASTRASSE1"].ToString()).Trim();
                    string KDASTRASSE2 = GetGB2312Value(dwKUNDDADR["KDASTRASSE2"].ToString()).Trim();
                    receiver += "收货地址:" + KDASTRASSE1 + Environment.NewLine;
                    receiver += "                " + KDASTRASSE2 + Environment.NewLine; 
                    phoneR += dwKUNDDADR["KDATTEX"].ToString().Trim();
                    phoneR += "/" + dwKUNDDADR["KDATEL"].ToString().Trim(); 
                    receiver+="收货人电话:"+phoneR;
                    continue;
                }
                
                exAgent.SetCellValue(row, 2,  receiver);
                DataRow dwVKKOPF = tbVKKOPF.Rows[0];
                query = from dwKDANSP in tbKDANSP.AsEnumerable()
                        where dwKDANSP["KDSKUNDNR"].ToString() == KUNDNR
                        & dwKDANSP["KDSANSPNA"].ToString() == dwVKKOPF["VKKANSPRECH"].ToString()
                        select dwKDANSP;
                string KDSANSPNAME = "";
                foreach (DataRow dwKDANSP in query)
                {
                    KDSANSPNAME = "收货人:" + GetGB2312Value(dwKDANSP["KDSANSPNAME"].ToString());
                }
                row = 10;
                KDSANSPNAME += "(" + phoneR + ")";
//                exAgent.SetCellValue(row, 2, KDSANSPNAME);
                #endregion

                #region=========发件人信息==============================
                row = 2;
                if (dsSource.Tables.Contains("HRSTAMM"))
                {
                    string name, tel;
                    string namekey;
                    string VKKSACHBE = dwVKKOPF["VKKSACHBE"].ToString();
                    string VKKNEUBEN = dwVKKOPF["VKKNEUBEN"].ToString();
                    foreach (DataRow dwHR in dsSource.Tables["HRSTAMM"].Rows)
                    {
                        namekey = dwHR["HRBENID"].ToString();
                        name =  GetGB2312Value(dwHR["HRVNAME"].ToString().Trim() + dwHR["HRNNAME"].ToString().Trim());
                        tel = "(" + dwHR["HRDTELH"].ToString().Trim() + "*" + dwHR["HRDTELD"].ToString().Trim() + ")";
                        if (namekey == VKKSACHBE)
                        {
                            
                            exAgent.SetCellValue(row, 21, "负责人:" +name + tel);
                        }
                        if (namekey == VKKNEUBEN)
                        {
                            exAgent.SetCellValue(row + 2, 21, "跟单员:"+name + tel);
                        }
                    }
                }
                row = 1;
                string VVLIEFSCH = "'" + System.Convert.ToInt64(dwVVLIEF["VVLIEFSCH"].ToString());
                exAgent.SetCellValue(row, 25, VVLIEFSCH);
                string printdate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
                exAgent.SetCellValue(row, 30, printdate);
                #endregion

                #region=========承运人==============================
                if (dsSource.Tables.Contains("VERSART"))
                {
                    foreach (DataRow dwTemp in dsSource.Tables["VERSART"].Rows)
                    {
                        string VBEZ = "承运人:" + GetGB2312Value(dwTemp["VBEZ"].ToString()).Trim();
                        row = 3;
                        exAgent.SetCellValue(row, 21, VBEZ);
                    }
                }

                if (dsSource.Tables.Contains("TXTKON"))
                {
                    row = 5;
                    string txt = "";
                    string txtShiper = "";
                    foreach (DataRow dwVVTXT in dsSource.Tables["TXTKON"].Rows)
                    {
                        txt = GetGB2312Value(dwVVTXT["TXTKTEXT"].ToString()).Trim();
                        if (txt == "")
                            continue;
                        txtShiper += txt + Environment.NewLine;
                    }
                    exAgent.SetCellValue(row, 21, txtShiper);
                }
                #endregion

                #region=========Header==============================
                row = 11;
                string VKPMEEH = dsSource.Tables["VKPOS"].Rows[0]["VKPMEEH"].ToString();
                exAgent.SetCellValue(row, 2, "销售单号");
                exAgent.SetCellValue(row, 7, "品种");
                exAgent.SetCellValue(row, 10, "色号");
                exAgent.SetCellValue(row, 13, "件数");
                exAgent.SetCellValue(row, 16, "匹数");
                exAgent.SetCellValue(row, 18, "数量(" + VKPMEEH + ")");
                exAgent.SetCellValue(row, 23, "重量(kg)");
                #endregion
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void HideNullRows(int pages)
        {
            try
            {
                exAgent.HideRows((pages + 1) * 25 + 1, 1, 500, 999);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void CheckDirectory(string root, string sub, string year, string month, string day)
        {
            try
            {
                string drlocal = root;
                string dr0 = "";

                #region========================LOCAL=====================
                drlocal = Path.Combine(PathHelper.ApplicationPath, @"Report");
                if (!Directory.Exists(drlocal))
                    Directory.CreateDirectory(drlocal);

                drlocal = Path.Combine(drlocal, "Sales");
                if (!Directory.Exists(drlocal))
                    Directory.CreateDirectory(drlocal);
                #endregion
                drlocal = root;
                drlocal = Path.Combine(drlocal, sub);
                if (!Directory.Exists(drlocal))
                    Directory.CreateDirectory(drlocal);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private string CreateFile(string orderno)
        {
            string filename = "";
            string sheetName = "";
            string Standard = "VV06";
            string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\DST\DSTStandard.xls");
            try
            {
                string pwd = "office2003";
                sheetName = orderno;
                exAgent.Open(filePath);
                exAgent.Company = strCompany;
                exAgent.strExcelFile = strExcelFile;
                string newfilename = orderno;
                exAgent.CreateNewSheet(newfilename, Standard, "发货单", pwd);
                filename = exAgent.FileName;
                exAgent.SetCurrentSheet(1);
                int maxrow = FillInEXCEL();
                exAgent.CopyWorkSheet(1, 2, "细码单");
                exAgent.SetCurrentSheet("细码单");
                for (int row = 1; row < maxrow; row++)
                {
                    exAgent.ShowRows(row, 1, row, 1);
                }
                exAgent.DeleteWorkSheet("Sheet1");
                exAgent.DeleteWorkSheet("Sheet2");
                exAgent.DeleteWorkSheet("Sheet3");
                exAgent.SetCurrentSheet(1);

                string localFile = exAgent.FileName;
                exAgent.Save();
                exAgent.Close();
                //System.Diagnostics.Process.Start("EXCEL.EXE", localFile);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return filename;
        }

        public void CheckFTPDir(string root, string sub, string KRS, string year, string month, string day, string ordernumber = "")
        {
            string createdtimefile = PathHelper.ApplicationPath + "\\ftp.txt";
            string newfile = "/创建于" + year + month + day;
            try
            {
                string drlocal = root;
                if (!ftpClient.DirectoryExists(drlocal))
                {
                    ftpClient.CreateDirectory(drlocal);
                }
                drlocal = drlocal + "/" + sub;
                if (!ftpClient.DirectoryExists(drlocal))
                {
                    ftpClient.CreateDirectory(drlocal);
                }
                drlocal = drlocal + "/" + KRS;
                if (!ftpClient.DirectoryExists(drlocal))
                {
                    ftpClient.CreateDirectory(drlocal);
                }
                drlocal = drlocal + "/" + year;
                if (!ftpClient.DirectoryExists(drlocal))
                {
                    ftpClient.CreateDirectory(drlocal);
                }
                drlocal = drlocal + "/" + month;
                if (!ftpClient.DirectoryExists(drlocal))
                {
                    ftpClient.CreateDirectory(drlocal);
                    ftpClient.PutFile(createdtimefile, drlocal + newfile);
                }
                if (ordernumber != "")
                {
                    drlocal = drlocal + "/" + ordernumber;
                    if (!ftpClient.DirectoryExists(drlocal))
                    {
                        ftpClient.CreateDirectory(drlocal);
                        ftpClient.PutFile(createdtimefile, drlocal + newfile);
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void FTPShareFile(string KRS, string year, string month, string day, string ordernumer, string fileType)
        {
            string fileabsname = ordernumer + "_" + DateTime.Now.Day.ToString() + "_"
                    + DateTime.Now.ToShortTimeString().Replace(":", "_").Replace(" ", "_").Replace("/", "_") + ".xls";
            string localFile = @"C:\Windows\Temp\" + fileabsname;
            string tempfile = "";
            try
            {
                ftpClient.Open();
                ftpClient.Login();
                string root = "/Sales/";
                ftpClient.SetCurrentDirectory("/");
                CheckFTPDir("/Sales", "发货单", KRS, year, month, day);
                string destfile;
                filename = root + "发货单/" + KRS + "/" + year + "/" + month + "/" + fileabsname;
                if (ftpClient.FileExists(filename))
                {
                    ftpClient.RemoveFile(filename);
                }

                #region==========产生文件，复制到服务器=======================
                destfile = filename;
                tempfile = CreateFile(fileabsname);
                localFile = tempfile;
                if (ftpClient.FileExists(destfile))
                {
                    ftpClient.RemoveFile(destfile);
                }
                ftpClient.PutFile(tempfile, destfile);
                localFile = @"C:\Windows\Temp\" + fileabsname;
                ftpClient.GetFile(filename, localFile, false);
                #endregion
                #region=========打开服务器上的文件进行浏览========================
                if (fileType == "OpenExcel" ||
                    fileType == "EXCEL"
                    )
                {
                    localFile = @"C:\Windows\Temp\" + fileabsname;
                    if (File.Exists(localFile))
                    {
                        File.Delete(localFile);
                    }
                    File.Move(tempfile, localFile);
                    System.Diagnostics.Process.Start("EXCEL.EXE", localFile);
                }
                #endregion
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                if (File.Exists(tempfile))
                {
                    File.Delete(tempfile);
                }
            }
        }
        public override void WriteExcelReport(string fileType)
        {
            bool IsFTP = true;
            try
            {
                #region==========准备程序===================
                strExcelFile = @"DST\DSTStandard.xls";
                DataRow dwVKPOS = dsSource.Tables["VKPOS"].Rows[0];
                string KRS = dsSource.Tables["VKPOS"].Rows[0]["VKPAUFKRS"].ToString().Trim();
                string LFD = dsSource.Tables["VKPOS"].Rows[0]["VKPAUFLFD"].ToString().Trim();
                long flfd = 0;
                flfd = System.Convert.ToInt64(LFD);
                string newfilename = KRS.Trim() + flfd.ToString();
                exAgent.CheckWorkBook(newfilename);
                #endregion
                if (IsFTP)
                {
                    DataRow dwVVLIEF = dsSource.Tables["VVLIEF"].Rows[0];
                    string year = dwVVLIEF["VVNEUDAT"].ToString().Substring(0, 4);
                    string month = dwVVLIEF["VVNEUDAT"].ToString().Substring(4, 2);
                    string day = dwVVLIEF["VVNEUDAT"].ToString().Substring(6, 2);
                    FTPShareFile(KRS, year, month, day, KRS + LFD, fileType);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

    }
}
